<template>
  <div class="userLogin-container">
    <div class="login-form">
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="password"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px">
          <van-button round block color="#404040" native-type="submit"
            >提交</van-button
          >
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { userLogin } from "@/request/api.js";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    onSubmit(values) {
      userLogin({
        username: values["用户名"],
        pwd: values["密码"],
      }).then((res) => {
        if (res.errno == 0) {
          let { token, userInfo } = res.data;
          this.$store.commit("setToken", token);
          this.$store.commit("saveUserInfo", userInfo);
          this.$toast("登录成功！");
          setTimeout(() => {
            this.$router.go(-1);
          }, 500);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.userLogin-container {
  margin-top: 80px;
}
.login-form {
  border-radius: 10px;
  width: 80%;
  margin: 0 auto;
}
</style>
